﻿using System;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;

public partial class Indicators_FormulaEdit : System.Web.UI.Page
{
    private string sEditStatus = "1";
    private IndexFormulaBLL bll = new IndexFormulaBLL();
    private string sID = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            try
            {
                sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
                if (sEditStatus == "2")
                {
                    sID = this.Request.QueryString["ID"].ToString().Trim();
                    IndexFormula model = bll.GetModel(int.Parse(sID));
                    if (model != null)
                    {
                        txtName.Text = model.IndexName;
                        txtFormula.Text = model.Formula;
                        DLLtype.Text = model.FormulaParent.ToString();
                        txtOutOf.Text = model.OutOf.ToString();
                    }
                }
            }
            catch
            {

            }
        }
    }
    protected void SaveData_ServerClick(object sender, EventArgs e)
    {
        IndexFormula model = new IndexFormula();
        HsTec.Mobile.Green.Util.FormulaAnalyse fa = new HsTec.Mobile.Green.Util.FormulaAnalyse();
        try
        {
            model.IndexName = txtName.Text.ToString().Trim();
            model.Formula = txtFormula.Text.ToString().Trim();
            model.FormulaParent = int.Parse(DLLtype.SelectedValue.ToString().Trim());
            model.OutOf = int.Parse(txtOutOf.Text == "" ? "0" : txtOutOf.Text);
            model.FormulaNo = String.Empty;
            model.FormulaSQL = fa.FormatFormula(txtFormula.Text.ToString().Trim().Replace("（", "(").Replace("）", ")"));
            model.IndexSQL = bll.SaveFormula(model.FormulaSQL, model.IndexName);
            if (model.IndexName.IndexOf("节能") >= 0)
            {
                model.YearState = "3";
            }
            else if (model.IndexName.IndexOf("使用") >= 0)
            {
                model.YearState = "2";
            }
            else
            {
                model.YearState = "1";
                if (model.IndexName.IndexOf("载频") >= 0)
                {
                    model.FormulaNo = "BaseChange";
                }
                else if (model.IndexName.IndexOf("综合") >= 0)
                {
                    model.FormulaNo = "EnergyChange";
                }
                else
                {
                    model.FormulaNo = "TrafficChange";
                }
            }
            model.FormulaChildCount = 0;
            model.IsProcedure = "0";
            sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();

            if (sEditStatus == "1")
            {

                int iback = bll.Add(model);
                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');window.close();</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
                    return;
                }
            }
            else
            {
                model.IF_ID = this.Request.QueryString["ID"].ToString().Trim() == "" ? 0 : int.Parse(this.Request.QueryString["ID"].ToString().Trim());
                int iback = bll.Update(model);
                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.close();</script>");  //window.opener.location.reload();
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                    return;
                }
            }
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('操作失败!');</script>");
            return;
        }
    }
}
